//////////////////////////////////////////ok
#include"stdafx.h"
#include "bochs.h"



void IA32_CPU::ROL_Eb(Ia32_Instruction_c *i)
{
  Bit8u op1_8, result_8;
  unsigned count;

  if (i->b1() == 0xc0)
    count = i->Ib();
  else if (i->b1() == 0xd0)
    count = 1;
  else // 0xd2
    count = CL;

  if (i->modC0()) 
  {
    op1_8 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1_8);
  }

  if ( (count & 0x07) == 0 ) 
  {
    if ( count & 0x18 ) 
	{
      unsigned bit0 = op1_8 & 1;
      set_CF(bit0);
      set_OF(bit0 ^ (op1_8 >> 7));
    }
    return;
  }
  
  count &= 0x07; // use only lowest 3 bits
  result_8 = (op1_8 << count) | (op1_8 >> (8 - count));

  if (i->modC0()) 
  {
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result_8);
  }
  else 
  {
    write_RMW_virtual_byte(result_8);
  }

  bx_bool temp_CF = (result_8 & 0x01);
  set_CF(temp_CF);
  set_OF(temp_CF ^ (result_8 >> 7));
}

void IA32_CPU::ROR_Eb(Ia32_Instruction_c *i)
{
  Bit8u op1_8, result_8;
  unsigned count;

  if (i->b1() == 0xc0)
    count = i->Ib();
  else if (i->b1() == 0xd0)
    count = 1;
  else // 0xd2
    count = CL;

  if (i->modC0()) 
  {
    op1_8 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1_8);
  }

  if ( (count & 0x07) == 0 ) 
  {
    if ( count & 0x18 ) 
	{
      unsigned bit6 = (op1_8 >> 6) & 1;
      unsigned bit7 = (op1_8 >> 7);
      set_CF(bit7);
      set_OF(bit7 ^ bit6);
    }
    return;
  }
  count &= 0x07; /* use only bottom 3 bits */

  result_8 = (op1_8 >> count) | (op1_8 << (8 - count));
  if (i->modC0()) 
  {
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result_8);
  }
  else 
  {
    write_RMW_virtual_byte(result_8);
  }

  bx_bool result_b7 = (result_8 & 0x80) != 0;
  set_CF(result_b7);
  if (count == 1)
    set_OF(((op1_8 ^ result_8) & 0x80) > 0);
}

void IA32_CPU::RCL_Eb(Ia32_Instruction_c *i)
{
  Bit8u op1_8, result_8;
  unsigned count;

  if (i->b1() == 0xc0)
    count = i->Ib();
  else if (i->b1() == 0xd0)
    count = 1;
  else // 0xd2
    count = CL;

  count = (count & 0x1f) % 9;

  if (i->modC0()) 
  {
    op1_8 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1_8);
  }
 
  if (! count) 
	  return;

  if (count==1) 
  {
    result_8 = (op1_8 << 1) | getB_CF();
  }
  else 
  {
    result_8 = (op1_8 << count) | (getB_CF() << (count - 1)) | (op1_8 >> (9 - count));
  }

  if (i->modC0()) 
  {
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result_8);
  }
  else 
  {
    write_RMW_virtual_byte(result_8);
  }

  bx_bool temp_CF = (op1_8 >> (8 - count)) & 0x01;
  set_CF(temp_CF);
  set_OF(temp_CF ^ (result_8 >> 7));
}

void IA32_CPU::RCR_Eb(Ia32_Instruction_c *i)
{
  Bit8u op1_8, result_8;
  unsigned count;

  if (i->b1() == 0xc0)
    count = i->Ib();
  else if (i->b1() == 0xd0)
    count = 1;
  else // 0xd2
    count = CL;

  count = (count & 0x1f) % 9;

  if (i->modC0()) 
  {
    op1_8 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1_8);
  }

  if (! count) 
	  return;

  result_8 = (op1_8 >> count) | (getB_CF() << (8 - count)) | (op1_8 << (9 - count));

  if (i->modC0()) 
  {
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result_8);
  }
  else 
  {
    write_RMW_virtual_byte(result_8);
  }

  set_CF((op1_8 >> (count - 1)) & 0x01);
  if (count == 1)
    set_OF(((op1_8 ^ result_8) & 0x80) > 0);
}

void IA32_CPU::SHL_Eb(Ia32_Instruction_c *i)
{
  Bit8u op1_8, result_8;
  unsigned count;

  if (i->b1() == 0xc0)
    count = i->Ib();
  else if (i->b1() == 0xd0)
    count = 1;
  else // 0xd2
    count = CL;

  count &= 0x1f;

  if (i->modC0()) 
  {
    op1_8 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1_8);
  }

  if (!count) 
	  return;

  result_8 = (op1_8 << count);

  if (i->modC0()) 
  {
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result_8);
  }
  else 
  {
    write_RMW_virtual_byte(result_8);
  }

  IA32_SET_FLAGS_OSZAPC_8(op1_8, count, result_8, IA32_INSTR_SHL8);
}


void IA32_CPU::SHR_Eb(Ia32_Instruction_c *i)
{
  Bit8u op1_8, result_8;
  unsigned count;

  if (i->b1() == 0xc0)
    count = i->Ib();
  else if (i->b1() == 0xd0)
    count = 1;
  else // 0xd2
    count = CL;

  count &= 0x1f;

  if (i->modC0()) 
  {
    op1_8 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1_8);
  }

  if (!count) 
	  return;

  result_8 = (op1_8 >> count);


  if (i->modC0()) 
  {
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result_8);
  }
  else 
  {
    write_RMW_virtual_byte(result_8);
  }

  IA32_SET_FLAGS_OSZAPC_8(op1_8, count, result_8, IA32_INSTR_SHR8);
}

void IA32_CPU::SAR_Eb(Ia32_Instruction_c *i)
{
  Bit8u op1_8, result_8;
  unsigned count;

  if (i->b1() == 0xc0)
    count = i->Ib();
  else if (i->b1() == 0xd0)
    count = 1;
  else // 0xd2
    count = CL;

  count &= 0x1f;

  if (i->modC0()) 
  {
    op1_8 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1_8);
  }

  if (!count) 
	  return;

  if (count < 8) 
  {
    if (op1_8 & 0x80) 
	{
      result_8 = (op1_8 >> count) | (0xff << (8 - count));
    }
    else 
	{
      result_8 = (op1_8 >> count);
    }
  }
  else 
  {
    if (op1_8 & 0x80) 
	{
      result_8 = 0xff;
    }
    else 
	{
      result_8 = 0;
    }
  }

  if (i->modC0()) 
  {
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result_8);
  }
  else 
  {
    write_RMW_virtual_byte(result_8);
  }

  IA32_SET_FLAGS_OSZAPC_8(op1_8, count, result_8, IA32_INSTR_SAR8);
}
